package com.sgcc.lms.openstack.handler;

import com.sgcc.lms.openstack.exception.HttpException;
import com.sgcc.lms.openstack.exception.NullTokenException;
import com.sgcc.lms.openstack.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-23 07:53
 * @Description: 全局统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private static final String LINE = "==========================================》{}";

    /**
    * @Author 李孟帅
    * @Description 自定义异常处理
    * @Date 2020/7/23
    **/
    @ExceptionHandler({NullTokenException.class, HttpException.class})
    @ResponseBody
    public ResultUtil customExceptionHandler(HttpServletRequest request,HttpException e){
        String uri = request.getRequestURI();
        log.error(uri+",异常" + LINE, e.getMessage());
        return ResultUtil.result(e.getStatusCode(),e.getMessage());
    }

    /**
    * @Author 李孟帅
    * @Description 系统异常处理
    * @Date 2020/7/23
    **/
    @ExceptionHandler({Exception.class,IOException.class})
    @ResponseBody
    public ResultUtil exceptionHandler(HttpServletRequest request,Exception e){
        String uri = request.getRequestURI();
        log.error(uri+",异常" + LINE, e.getMessage());
        e.printStackTrace();
        return ResultUtil.result(500,e.getMessage());
    }
}
